<?php
defined('IN_WZ') or exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: 闲道人阿力
 * Date: 2016/8/25
 * Time: 14:35
 */
class api
{
    private $code = "aaacccxddd111bbbsssccc009ip";

    function __construct()
    {

//        if($GLOBALS['code']!=$this->code){
//            $r['status'] = 1 ;
//            $r['msg'] = "加密码错误";
//            exit(json_encode($r));
//
//
//
//        }


    }


    public function updatetargetmail(){
        $db = load_class("mydb","api");
        $c['id']=$GLOBALS['id'];
        $d['targetmail'] = $GLOBALS['targetmail'];
        $db->begin()->where($c)->save("ziduan",$d);

        $r['status'] = 0 ;
        $r['msg'] = "ok3 ";
        echo json_encode($r);



    }
    public function updatetixing(){
        $db = load_class("mydb","api");
        $c['id']=$GLOBALS['id'];
        $d['tixing'] = $GLOBALS['tixing'];
        $db->begin()->where($c)->save("ziduan",$d);

        $r['status'] = 0 ;
        $r['msg'] = "ok ";
        echo json_encode($r);



    }
    public function updatediscribe(){
        $db = load_class("mydb","api");
        $c['id']=$GLOBALS['id'];
        $d['discribe'] = $GLOBALS['discribe'];
        $db->begin()->where($c)->save("ziduan",$d);

        $r['status'] = 0 ;
        $r['msg'] = "ok ";
        echo json_encode($r);
    }


    public function getuid(){
        $db = load_class('db');
        $uid = get_cookie('_uid');
        if($uid)
        {
            echo $uid;
        }else{
            echo 0;
        }
    }

    /**
     * 获取验证码的值
     */
    public function getcode(){


        if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){
            // ajax 请求的处理方式
        }else{

            exit( "invalid call 1!");
        };

        load_class("session");
        echo $_SESSION['code'];
    }

    public function ziduanaction()
    {

        //$dataarr = $GLOBALS['data'];
        //$dataarr =str_replace(" ","",str_replace(chr(13),chr(32),$dataarr));
        //$dataarr = explode("\n",$dataarr);








        //dump($dataarr);

        //dump($GLOBALS);





        $mydb = load_class("mydb","api");

        $table = $GLOBALS['table'];
        $form = $GLOBALS['form'];
        $d['dbname'] = $table ;
        $cums = $mydb->begin()->where($d)->getall("ziduanx");
        $dbs = $mydb->begin()->where($d)->getall("ziduan");
        $subject = "自定义字段【".$dbs[0]["discribe"]."】有新的信息";
        $tomail = $dbs[0]["targetmail"];
        //echo $subject ;
        $body = "";
        foreach ($cums as $r)
        {


            if(is_array($form[$r['dbcum']])){
                $c[$r['dbcum']]= json_encode($form[$r['dbcum']]);
                $body .= $r['dbdescribe']."(".  $r['dbcum'].")=>".json_encode( $form[$r['dbcum']])."<br>";
            }else{

                $c[$r['dbcum']]= $form[$r['dbcum']];
                $body .= $r['dbdescribe']."(".  $r['dbcum'].")=>".$form[$r['dbcum']]."<br>";
            }


        }

        $c['createtime'] = date('Y-m-d H:i:s',time());
        $uid = get_cookie('_uid');
        if($uid){
            $c['uid']=$uid;
        }
        //echo $body ;

        $mydb->add($table,$c);

        if($dbs[0]["tixing"]==1)
        {
            load_function('sendmail');
            $mailarr = explode(";", $tomail);
            foreach ($mailarr as $mails) {
                send_mail($mails,$subject,$body);
            }
        }else if($dbs[0]["tixing"]==2)
        {
            $send = load_class("Mail","api");

            $mailarr = explode(";", $tomail);
            foreach ($mailarr as $mails) {
                $send->send($mails,$subject,$body);
            }
        }

        MSG($GLOBALS['msg'],HTTP_REFERER,500);



    }







}